package com.currency.mall.ui.my.capital;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.currency.mall.R;
import com.currency.mall.adapter.CapitalStatusAdapter;
import com.currency.mall.di.component.DaggerCapitalDetailsComponent;
import com.currency.mall.di.modlue.CapitalDetailsModule;
import com.currency.mall.entity.CapitalFlowBean;
import com.currency.mall.entity.CapitalOder;
import com.mifengs.commonbase.base.BaseFragment;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/12/24.
 */

public class CapitalDetailsFragment extends BaseFragment<CapitalDetailsPresent> implements CapitalDetailsContract.View {
    @BindView(R.id.rv_capital_statu)
    RecyclerView rvCapitalStatu;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    @BindView(R.id.ll_not_data)
    RelativeLayout llNotData;
    private CapitalStatusAdapter mCapitalStatusAdapter;
    private int mCurrentPage;
    private int position;//当前位置
    private String status;//资金状态
    private List<CapitalOder> capitalOders = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_capital_status, container, false);
        unbinder = ButterKnife.bind(this, mView);
        position = FragmentPagerItem.getPosition(getArguments());
        init();
        return mView;

    }

    @Override
    public void initInjector() {
        DaggerCapitalDetailsComponent.builder()
                .capitalDetailsModule(new CapitalDetailsModule(this))
                .build()
                .inject(this);
    }

    private void init() {
        setStatus();
        rvCapitalStatu.setLayoutManager(new LinearLayoutManager(getContext()));
        rvCapitalStatu.setNestedScrollingEnabled(false);
        refreshLayout.setEnableLoadMore(false);
        mCapitalStatusAdapter = new CapitalStatusAdapter(null, getContext());
        rvCapitalStatu.setAdapter(mCapitalStatusAdapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (refreshLayout != null) {
                    setStatus();
                    mCurrentPage = 1;
                    mPresenter.getCapitalDetailsData(mCurrentPage, status);
                }
            }
        });

//        mCapitalStatusAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                CapitalOder capitalOder = capitalOders.get(position);
//                Intent intent = null;
//                if("RECHARGE".equals(capitalOder.getType())){
//                    Subscribe subscribe = new Gson().fromJson(capitalOder.getRemark(),Subscribe.class);
//                    intent = new Intent(getContext(),CapitalDetailsActivity.class);
//                }else if("CASH".equals(capitalOder.getType())){
//                    intent = new Intent(getContext(),WithdrawalDetailsActivity.class);
//                }else if("PURCHASE".equals(capitalOder.getType())){
//                    intent = new Intent(getContext(),CapitalDetailsActivity.class);
//                }else if("SETTLE".equals(capitalOder.getType())){
//                    intent = new Intent(getContext(),EntrustBalanceDetailsActivity.class);
//                }
//            }
//        });
        mPresenter.getCapitalDetailsData(1, status);
    }

    private void setStatus() {
        if (position == 0) {
            status = "";
        } else if (position == 1) {
            status = "INCOME";
        } else if (position == 2) {
            status = "DISBURSE";
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void setData(CapitalFlowBean.UserAccount userAccount) {
        capitalOders = userAccount.getList();
        if(capitalOders.size() == 0){
            llNotData.setVisibility(View.VISIBLE);
        }else{
            llNotData.setVisibility(View.GONE);
        }
        mCapitalStatusAdapter.setNewData(capitalOders);

    }

    @Override
    public void finishRefresh() {
        if (refreshLayout != null)
            refreshLayout.finishRefresh();
    }

    @Override
    public int position() {
        return position;
    }
}
